﻿HmJS.$Import('log');
HmJS.$Import('lang.class');

/**
* @module Class.Mutators.Attributes
* @description allows to add attributes with setters, getters, validators and some more help methods
**/

HmJS.register('demo.module.lang.class.attributes', function ($ns) {
	var that = {};

	var Product = new Class({

		Implements: [Options, Events],

		Attributes: {

			hmkhan: {
				valueFn: function () {
					return 'my name is HmKhan';
				}
			},

			brand: {
				validator: function (val) {
					return val.trim().length > 1;
				}
			},

			model: {
				validator: function (val) {
					return val.trim().length > 1;
				}
			},

			name: {
				readOnly: true,
				getter: function () {
					return this.get('brand') + ' ' + this.get('model');
				}
			},

			price: {
				getter: function (val) {
					return val * (100 - this.get('discount')) / 100
				}
			},

			discount: {
				value: 0 // Default value
			}

		},

		initialize: function (attributes) {
			this.setAttributes(attributes);
		}

	});

	var product = new Product({
		brand: 'Porsche',
		model: '911',
		price: 100000,
		discount: 5
	});

	that.product = product;

	that.todo = function () {

		log.info(product.get('name'));
		log.info(product.get('price'));
		product.addEvent('discountChange', function (event) {
			log.info("New discount: {newVal}% instead of {oldVal}%!".substitute(event));
		});

		product.set('discount', 30); // -> alerts "New discount: 30% instead of 5!"
		log.info(product.get('discount'));

		product.addAttribute('model', { value: '110' });
		log.info(product.get('model'));

		log.info(product.get('hmkhan'));
	}

	return that;
});
